package com.uchiha.nova.mainService.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.dto.CompanyBusinessLicenseDTO;
import cn.hutool.core.bean.BeanUtil;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.CompanyBusinessLicenseEntity;
import com.uchiha.nova.core.model.vo.CompanyBusinessLicenseVO;
import com.uchiha.nova.core.model.query.CompanyBusinessLicenseQuery;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.ICompanyBusinessLicenseService;
import com.uchiha.nova.mainService.wrapper.CompanyBusinessLicenseWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 公司营业执照信息 控制器
 *
 * @author Uchihashiamu
 */
@RestController
@AllArgsConstructor
@RequestMapping("/companyBusinessLicense")
@Tag(name = "公司营业执照信息")
@Slf4j
public class CompanyBusinessLicenseController extends BaseController {

    @Resource
    ICompanyBusinessLicenseService companyBusinessLicenseService;

    /**
     * 详情
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
    @Operation(summary = "详情")
    public R<CompanyBusinessLicenseVO> getOne(@RequestBody CompanyBusinessLicenseDTO companyBusinessLicenseDTO) {
        CompanyBusinessLicenseEntity entity = new CompanyBusinessLicenseEntity();
        BeanUtil.copyProperties(companyBusinessLicenseDTO, entity);
        CompanyBusinessLicenseEntity detail = companyBusinessLicenseService.getOne(Condition.getQueryWrapper(entity));
        return R.data(CompanyBusinessLicenseWrapper.build().entityVO(detail));
    }

    @GetMapping("/getByName")
    public R<CompanyBusinessLicenseVO> getByName(@RequestParam("name") String name) {
        CompanyBusinessLicenseEntity entity = new CompanyBusinessLicenseEntity();
        entity.setName(name);
        CompanyBusinessLicenseEntity detail = companyBusinessLicenseService.getOne(Condition.getQueryWrapper(entity));
        if(ObjectUtil.isEmpty(detail)) {
            String url = "https://open.api.tianyancha.com/services/open/ic/baseinfo/normal?keyword="+name;
            Map<String,String> header = new HashMap<String,String>();
            header.put("Authorization","cde76730-a6dc-4476-a020-f6b393fd6bef");
            HttpResponse response = HttpUtil.createGet(url).addHeaders(header).execute();
            if(200 != response.getStatus()){
                return R.fail("远程验证失败");
            }else{
                log.info("远程验证:{}",response.body());
                JSONObject jsonObject = JSONObject.parseObject(response.body());
                Integer code = jsonObject.getInteger("error_code");
                if(0==code){
                    CompanyBusinessLicenseEntity entityRes = jsonObject.getObject("result", CompanyBusinessLicenseEntity.class);
                    entityRes.setState(1);
                    companyBusinessLicenseService.save(entityRes);
                    return R.data(CompanyBusinessLicenseWrapper.build().entityVO(entityRes));
                }else{
                    CompanyBusinessLicenseEntity entityResNo = new CompanyBusinessLicenseEntity();
                    entityResNo.setState(0);
                    entityResNo.setName(name);
                    companyBusinessLicenseService.save(entityResNo);
                    return R.fail("请核对企业名称");
                }

            }

        }else{
            if(0==detail.getState()){
                return R.fail("请核对企业名称");
            }else{
                return R.data(CompanyBusinessLicenseWrapper.build().entityVO(detail));
            }
        }
    }

    /**
     * 分页 代码自定义代号
     */
    @GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
    @Operation(summary = "分页查询")
    public R getPage(CompanyBusinessLicenseQuery query, QueryContext queryContext) {
        IPage<CompanyBusinessLicenseEntity> pages = companyBusinessLicenseService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext, CompanyBusinessLicenseEntity.class));
        return R.data(CompanyBusinessLicenseWrapper.build().pageVO(pages));
    }

    /**
     * 新增 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
    @Operation(summary = "新增")
    public R save(@RequestBody CompanyBusinessLicenseDTO companyBusinessLicenseDTO) {
        CompanyBusinessLicenseEntity entity = new CompanyBusinessLicenseEntity();
        BeanUtil.copyProperties(companyBusinessLicenseDTO, entity);
        return R.status(companyBusinessLicenseService.save(entity));
    }

    /**
     * 修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)
    @Operation(summary = "修改")
    public R update(@RequestBody CompanyBusinessLicenseDTO companyBusinessLicenseDTO) {
        CompanyBusinessLicenseEntity entity = new CompanyBusinessLicenseEntity();
        BeanUtil.copyProperties(companyBusinessLicenseDTO, entity);
        return R.status(companyBusinessLicenseService.updateById(entity));
    }

    /**
     * 新增或修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
    @Operation(summary = "新增或修改")
    public R saveOrUpdate(@RequestBody CompanyBusinessLicenseDTO companyBusinessLicenseDTO) {
        CompanyBusinessLicenseEntity entity = new CompanyBusinessLicenseEntity();
        BeanUtil.copyProperties(companyBusinessLicenseDTO, entity);
        return R.status(companyBusinessLicenseService.saveOrUpdate(entity));
    }


    /**
     * 物理删除，不可恢复
     */
    @Operation(summary = "物理删除")
    @DeleteMapping(NovaCRUDMethodConstant.NOVA_WEB_DELETE)
    public R delete(@RequestParam String ids) {
        return R.status(companyBusinessLicenseService.deletePhysics(Func.toLongList(ids)));
    }
}
